<?php

class Damages_Methods_Request_SetCarDataDealer extends Damages_Methods_Request {

  /**
   *
   * @var int
   */
  public $id;

  /**
   *
   * @var int
   */
  public $dealerId;

  public function __construct($params) {
    parent::__construct($params);
    $this->id = (int) $params->id;
    $this->dealerId = (int) $params->dealerId;
  }

  public function process() {
    // manualna zmiana dealera
    if (!Damages_Auth::getInstance()->checkPermission(array(Damages_Auth::PERMISSION_LS, Damages_Auth::PERMISSION_ADMIN)))
      throw new Damages_Exception_App(Damages_Codes::ACCESS_DENIED);
    $dealer = DamagesDealersQuery::create()->findOneById($this->dealerId);
    if (!$dealer instanceof DamagesDealers)
      throw new Damages_Exception_App(Damages_Codes::DB_OBJECT_NOT_FOUND);
    $parentId = $dealer->getParentId();
    $data = $this->getDbObject();
    if ($data) {
      $data->setDealerId($this->dealerId);
      $data->setDealerParentId($parentId);
      $data->setLocked(1);
      $data->save();
    }
    else
      throw new Damages_Exception_App(Damages_Codes::DB_OBJECT_NOT_FOUND);
    $response = new Damages_Methods_Response(true);
    return $response;
  }

  /**
   * 
   * @return DamagesCarData
   */
  private function getDbObject() {
    $data = DamagesCarDataQuery::create()->findOneById($this->id);
    return $data;
  }

}